package com.yangxk.kadmin.web.module.system.service.impl;

import com.yangxk.kadmin.common.base.AbstractService;
import com.yangxk.kadmin.common.exception.BizException;
import com.yangxk.kadmin.common.exception.ParamException;
import com.yangxk.kadmin.common.support.BeanValidator;
import com.yangxk.kadmin.common.support.generator.IdGenerator;
import com.yangxk.kadmin.web.common.enumeration.ResponseCode;
import com.yangxk.kadmin.web.module.system.form.DictEditForm;
import com.yangxk.kadmin.web.module.system.mapper.DictMapper;
import com.yangxk.kadmin.web.module.system.po.Dict;
import com.yangxk.kadmin.web.module.system.service.DictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictServiceImpl extends AbstractService<Dict, Long> implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Override
    public List<Dict> queryList(long parentId) {

        List<Dict> dictList = dictMapper.findByParentId(parentId);

        return dictList;
    }

    @Transactional
    @Override
    public long addDict(DictEditForm form) {
        BeanValidator.check(form);
        Dict dict = new Dict();
        BeanUtils.copyProperties(form, dict);

        if (dict.getParentId() == null)
            dict.setParentId(DICT_TYPE_PARENTID);

        dict.setId(IdGenerator.getId());
        dict.setCreateTime(new Date());
        dict.setUpdateTime(new Date());

        dictMapper.insertSelective(dict);

        return dict.getId();
    }

    @Override
    @Transactional
    public long updateDict(DictEditForm updateForm) {
        BeanValidator.check(updateForm);

        if (updateForm.getId() == null)
            throw new ParamException("id不能为空");

        Dict dict = dictMapper.selectByPrimaryKey(updateForm.getId());
        if (dict == null)
            throw new BizException(ResponseCode.UPDATE_CHECK_NULL);

        Dict update = new Dict();
        BeanUtils.copyProperties(updateForm, update);

        if (dict.getParentId() == null)
            dict.setParentId(DICT_TYPE_PARENTID);
        dict.setUpdateTime(new Date());

        dictMapper.updateByPrimaryKeySelective(update);

        return dict.getId();
    }

    @Override
    @Transactional
    public void deleteDict(long id) {
        Dict dict = dictMapper.selectByPrimaryKey(id);
        if (dict == null)
            throw new BizException(ResponseCode.UPDATE_CHECK_NULL);

        dictMapper.deleteByPrimaryKey(id);
        // 删除子节点
        dictMapper.deleteByParentId(dict.getId());
    }

    @Override
    public Map<String, List<Dict>> getAll() {
        List<Dict> parents = queryList(DICT_TYPE_PARENTID);

        Map<String, List<Dict>> result = new HashMap<>(parents.size());
        for (Dict parent : parents) {
            result.put(parent.getCode(), queryList(parent.getId()));
        }

        return result;
    }
}
